<?php
class Datphong extends CI_Controller {

    function khachsan($makhachsan) {
        $this->load->model('datphong_model');
        $this->load->model('khachsan_model');
        $this->load->model('loaiphong_model');
        $this->load->model('hinhanh_model');		
        $data['country'] = $this->khachsan_model->layQuocGia();
        $data['records'] = $this->loaiphong_model->layloaiphong($makhachsan);
        $data['records1'] = $this->datphong_model->laygia_index($makhachsan);
        $data['khachsan'] = $this->khachsan_model->get_records_khachsan_($makhachsan);
		$data['hinhanh'] = $this->hinhanh_model->LayHinhAnh($makhachsan);
        $data['ngay'] = date('j');
        $data['thang'] = date('n');
        $data['nam'] = date('Y');
        $data['songay'] = date('t');
        $data['thu'] = date('w') + 1;
        $this->load->view('home/datphong_view', $data);
    }
	
	function chitiet($MaPhieuDat) {
	$this->load->model('datphong_model');
        $this->load->model('khachsan_model');
        $this->load->model('datphong_model');		
        $data['country'] = $this->khachsan_model->layQuocGia();
		$data['ThongTinDatPhong'] = $this->datphong_model->ThongTinDatPhong($MaPhieuDat);
        $this->load->view('home/ThongTinDatPhong_view', $data);
    }
	
    function khachsantrongvung() 
	{
		$select_country = urldecode($this->uri->segment(3));
		$select_city = urldecode($this->uri->segment(4));
		
        $this->load->model('khachsan_model');
        $data['country'] = $this->khachsan_model->layQuocGia();
        $data['DSKhachSan'] = $this->khachsan_model->TimKhachSanTrongVung($select_city);

		$data['select_date'] = date("Y-m-d");
		
		$data['city'] = $this->khachsan_model->TimThanhPho($select_country);
		$data['region'] = $this->khachsan_model->TimQuan($select_city);
		
		$data['select_hotel'] = "Nhập tên thành phố hoặc tên khách sạn...";				
		$data['select_country'] = $select_country;
		$data['select_city'] = $select_city;
		$data['select_region'] = NULL;
  
		$this->load->view('home/SearchKS_view', $data);	
    }

    function home() {
        $this->load->model('khachsan_model');
        $data['khachsandangcokhuyenmai'] = $this->khachsan_model->khachsandangcokhuyenmai();
        $data['khachsantotnhat'] = $this->khachsan_model->khachsantotnhat();
        $data['khachsantieubieu'] = $this->khachsan_model->khachsantieubieu();
        $this->load->view('home/TrangChu_view', $data);
    }

    function kiemtradatphong() {
        $this->load->model('datphong_model');
        $data['ketqua'] = $this->datphong_model->kiemtradatphong();
        $this->load->view('home/kiemtradatphong_ajax', $data);
    }

    function loadbanggia() {
        $this->load->model('datphong_model');
        $this->load->model('loaiphong_model');

        $makhachsan = $this->input->post('khachsan');
        $data['records'] = $this->loaiphong_model->layloaiphong($makhachsan);
        $data['records1'] = $this->datphong_model->laygia($makhachsan);
        $data['ngay'] = $this->input->post('ngay');
        $data['thang'] = $this->input->post('thang');
        $data['nam'] = $this->input->post('nam');
        $data['songay'] = $this->input->post('songay');
        $data['thu'] = $this->input->post('thu');

        $data['ngaybatdau'] = $this->input->post('ngaybatdau');
        $data['thangbatdau'] = $this->input->post('thangbatdau');
        $data['nambatdau'] = $this->input->post('nambatdau');

        $data['ngayketthuc'] = $this->input->post('ngayketthuc');
        $data['thangketthuc'] = $this->input->post('thangketthuc');
        $data['namketthuc'] = $this->input->post('namketthuc');

        $data['date'] = $this->input->post('date');

        $this->load->view('home/banggia_ajax', $data);
    }

    function loadbanggiatheongay() {
        $date = $this->input->post('ngay');
        $date = explode('/', $date);
        $ts = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
        $date = date("Y-m-d", $ts);
        $startdate = date("Y-m-d");
        $enddate = $startdate;
        $middate = $startdate;
        $date = date_create($date);
        $startdate = date_create($startdate);
        $enddate = date_create($enddate);
        $middate = date_create($middate);
        date_add($enddate, date_interval_create_from_date_string('13 days'));
        date_add($middate, date_interval_create_from_date_string('10 days'));
        //$i = 0;
        while ($date > $enddate) {
            date_add($startdate, date_interval_create_from_date_string('7 days'));
            date_add($middate, date_interval_create_from_date_string('7 days'));
            date_add($enddate, date_interval_create_from_date_string('7 days'));
            //$i = $i+1;
        }
        if ($date >= $middate) {
            date_add($startdate, date_interval_create_from_date_string('7 days'));
            date_add($enddate, date_interval_create_from_date_string('7 days'));
        }
        $startdate = date_format($startdate, 'd/m/Y');
        $date = explode('/', $startdate);
        $ts = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
        $makhachsan = $this->input->post('khachsan');
        $this->load->model('datphong_model');
        $this->load->model('loaiphong_model');
        $data['records'] = $this->loaiphong_model->layloaiphong($makhachsan);
        $data['records1'] = $this->datphong_model->laygiangay($makhachsan, $date[0], $date[1], $date[2], date('t', $ts));
        $data['ngay'] = $date[0];
        $data['thang'] = $date[1];
        $data['nam'] = $date[2];
        $data['date'] = $this->input->post('ngay');
        $data['songay'] = date('t', $ts);
        $data['thu'] = $this->input->post('thu');

        $data['ngaybatdau'] = 0;
        $data['thangbatdau'] = 0;
        $data['nambatdau'] = 0;

        $data['ngayketthuc'] = 0;
        $data['thangketthuc'] = 0;
        $data['namketthuc'] = 0;

        $this->load->view('home/banggia_ajax', $data);
    }

    function chitietdat() {
		$maks = $this->input->post('makhachsan');
		
        $this->load->model('datphong_model');
        $this->load->model('khachsan_model');
        $this->load->model('loaiphong_model');
        $this->load->model('hinhanh_model');		
		
        $data['khachsan'] = $this->khachsan_model->get_records_khachsan();
        $data['loaiphong'] = $this->loaiphong_model->get_records_loaiphong();
		$data['hinhanh'] = $this->hinhanh_model->LayHinhAnh($maks);		

        $data['records'] = $this->datphong_model->laygia_chitiet();
        $data['ngaybatdau'] = $this->input->post('ngaybatdau');
        $data['thangbatdau'] = $this->input->post('thangbatdau');
        $data['nambatdau'] = $this->input->post('nambatdau');

        $data['ngayketthuc'] = $this->input->post('ngayketthuc');
        $data['thangketthuc'] = $this->input->post('thangketthuc');
        $data['namketthuc'] = $this->input->post('namketthuc');
        $this->load->view('home/chitietdat_view', $data);
    }

    function loadbanggia_chitiet() {
        $this->load->model('datphong_model');
        $this->load->model('khachsan_model');
        $this->load->model('loaiphong_model');
        $data['khachsan'] = $this->khachsan_model->get_records_khachsan();
        $data['loaiphong'] = $this->loaiphong_model->get_records_loaiphong();
        $data['records'] = $this->datphong_model->laygia_chitiet();

        $data['ngaybatdau'] = $this->input->post('ngaybatdau');
        $data['thangbatdau'] = $this->input->post('thangbatdau');
        $data['nambatdau'] = $this->input->post('nambatdau');

        $data['ngayketthuc'] = $this->input->post('ngayketthuc');
        $data['thangketthuc'] = $this->input->post('thangketthuc');
        $data['namketthuc'] = $this->input->post('namketthuc');

        $data['songuoilon1'] = $this->input->post('songuoilon1');
        $data['sotreem1'] = $this->input->post('sotreem1');

        $data['songuoilon2'] = $this->input->post('songuoilon2');
        $data['sotreem2'] = $this->input->post('sotreem2');

        $data['songuoilon3'] = $this->input->post('songuoilon3');
        $data['sotreem3'] = $this->input->post('sotreem3');

        $data['songuoilon4'] = $this->input->post('songuoilon4');
        $data['sotreem4'] = $this->input->post('sotreem4');

        $data['songuoilon5'] = $this->input->post('songuoilon5');
        $data['sotreem5'] = $this->input->post('sotreem5');

        $data['songuoilon6'] = $this->input->post('songuoilon6');
        $data['sotreem6'] = $this->input->post('sotreem6');

        $data['songuoilon7'] = $this->input->post('songuoilon7');
        $data['sotreem7'] = $this->input->post('sotreem7');

        $data['songuoilon8'] = $this->input->post('songuoilon8');
        $data['sotreem8'] = $this->input->post('sotreem8');

        $data['songuoilon9'] = $this->input->post('songuoilon9');
        $data['sotreem9'] = $this->input->post('sotreem9');

        $data['songuoilon10'] = $this->input->post('songuoilon10');
        $data['sotreem10'] = $this->input->post('sotreem10');

        $data['sophong'] = $this->input->post('sophong');

        $data['phantramgiam'] = $this->input->post('phantramgiam');
        $data['diem'] = $this->input->post('diem');
        $data['sothe'] = $this->input->post('sothe');
        $this->load->view('home/banggia_chitiet_ajax', $data);
    }

    function loadthekhuyenmai() {
        $this->load->model('thekhuyenmai_model');
        $data['tongtien'] = $this->input->post('tongtien');
        $data['thekhuyenmai'] = $this->thekhuyenmai_model->get_records_the_i12();

        $this->load->view('home/thei12_ajax', $data);
    }

    function xulidatphong() {
        $this->load->model('phieudatphong_model');
        $this->load->model('chitietphieudat_model');
        $this->load->model('giathuetheongay_model');
        $this->load->model('phongtrongtheongay_model');
		$this->load->model('thekhuyenmai_model');
        //$ts = mktime(0, 0, 0, 12, 31, 2012);
//        $date = date("Y-m-d", $ts);
//        $date = date_create($date);
//        date_add($date, date_interval_create_from_date_string('1 days'));
//        echo $date;
        $ngaybatdau = $this->input->post('ngaybatdau');
        $thangbatdau = $this->input->post('thangbatdau');
        $nambatdau = $this->input->post('nambatdau');

        $ngayketthuc = $this->input->post('ngayketthuc');
        $thangketthuc = $this->input->post('thangketthuc');
        $namketthuc = $this->input->post('namketthuc');

        $ts = mktime(0, 0, 0, $thangbatdau, $ngaybatdau, $nambatdau);
        $startdate = date("Y-m-d", $ts);
        $startdate = date_create($startdate);

        $ts = mktime(0, 0, 0, $thangketthuc, $ngayketthuc, $namketthuc);
        $enddate = date("Y-m-d", $ts);
        $enddate = date_create($enddate);

        $sotienthemnguoihienhanh = $this->input->post('sotienthemnguoi');
        $songuoilonmacdinh = $this->input->post('songuoilonmacdinh');
        $sophong = $this->input->post('numberRooms');

        $khachsan = $this->input->post('makhachsan');
        $loaiphong = $this->input->post('loaiphong');

        $phantramgiam = $this->input->post('phantramgiam');
        $diem = $this->input->post('diem');
        $sothe = $this->input->post('sothe');

        $gionhanphong = $this->input->post('eta');
        $email = $this->input->post('emailAddress');
        $dienthoai = $this->input->post('phoneNumber');

        $ghichu = $this->input->post('comments');
        $tongtien = $this->input->post('totalconlai');
        $tinhtrangthanhtoan = $this->input->post('tinhtrangthanhtoan');

        $ngaydatphong = date("Y-m-d H:i:s");

        $data = array(
            'Email' => $email,
            'DienThoai' => $dienthoai,
            'NgayDatPhong' => $ngaydatphong,
            'GioNhanPhong' => $gionhanphong,
            'PhanTramGiam' => $phantramgiam,
            'TongTien' => floor($tongtien/1000),
            'TinhTrangThanhToan' => $tinhtrangthanhtoan,
            'LoaiPhong' => $loaiphong);
        if ($sothe != 0)
        {
			$data['SoThe'] = $sothe;
			$this->thekhuyenmai_model->update_records_the_i12($tongtien);
		}
        if ($ghichu != '')
            $data['GhiChu'] = $ghichu;

        $id = $this->phieudatphong_model->add_records_phieudatphong($data);

        for ($i = 1; $i <= $sophong; $i++) {
            $songuoilon = $this->input->post('room' . $i . 'NumberAdults');
            $sotreem = $this->input->post('room' . $i . 'NumberChildren');
            $ho = $this->input->post('room' . $i . 'GuestLastName');
            $ten = $this->input->post('room' . $i . 'GuestFirstName');
            $loaigiuong = $this->input->post('room' . $i . 'Option');

            if ($songuoilon > $songuoilonmacdinh)
                $sotienthemnguoi = $sotienthemnguoihienhanh * ($songuoilon - $songuoilonmacdinh);
            else
                $sotienthemnguoi = 0;
            $datact = array(
                'HoTenNguoiDaiDien' => $ho . ' ' . $ten,
                'SoNguoiLon' => $songuoilon,
                'SoTreEm' => $sotreem,
                'SoTienThemNguoi' => $sotienthemnguoi / 1000,
                'LoaiGiuong' => $loaigiuong,
                'PhieuDatPhong' => $id);
            $this->chitietphieudat_model->add_records_chitietdatphong($datact);
            $this->phongtrongtheongay_model->update_records_phongtrongtheongay($startdate, $enddate, $loaiphong, $loaigiuong);
        }
		$ts = mktime(0, 0, 0, $thangbatdau, $ngaybatdau, $nambatdau);
        $startdate = date("Y-m-d", $ts);
        $startdate = date_create($startdate);

        $ts = mktime(0, 0, 0, $thangketthuc, $ngayketthuc, $namketthuc);
        $enddate = date("Y-m-d", $ts);
        $enddate = date_create($enddate);
        $this->giathuetheongay_model->add_records_giathuetheongay($startdate, $enddate, $id, $loaiphong);
		if($tinhtrangthanhtoan == 1)
		{
		// Gửi email thanh toán thành công
					$emailfrom = 'i12.onlinebooking@gmail.com';
					$emailpass = 'onlinebooking';
					$sender = 'i12 Online Booking';
				
					$emailto = $this->phieudatphong_model->LayEmail($id);
					$subject = "Đặt phòng thành công";
					$message = "Chúc mừng bạn, bạn đã đăng ký và thanh toán thành công. \r\n Công ty xin chân thành cảm ơn bạn đã đặt niềm tin nơi chúng tôi. \r\n Mã số đặt phòng của bạn là ".$id.". Vui lòng ghi nhớ mã đặt phòng này!!!";
				
					$config = array(
									'protocol' => 'smtp',
									'smtp_host' => 'ssl://smtp.googlemail.com',
									'smtp_port' => 465,
									'smtp_user' => $emailfrom,
									'smtp_pass' => $emailpass,
									'wordwrap' => FALSE
									);
								
					$this->load->library('email', $config);
					$this->email->set_newline("\r\n");
				
					$this->email->from($emailfrom, $sender);
					$this->email->to($emailto);
					$this->email->subject($subject);
					$this->email->message($message);
				
					if($this->email->send())
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
    		    	    <script>
							alert("Thanh toán thành công. Thư xác nhân đặt phòng đã được gửi đi.");
							window.location = "<?php echo base_url(); ?>index.php/datphong/chitiet/<?php echo $id; ?>";
						</script>
			            <?php					
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	    		        <script>
							alert("Gửi thư xác nhận đặt phòng thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/datphong/chitiet/<?php echo $id; ?>";
						</script>
			            <?php					
					}
		}else{
		redirect('datphong/chitiet/'.$id);
		}
    }

}
?>